@echo off
chcp 65001 >nul

REM 多实例服务启动脚本
REM 用于同时启动多个服务实例

echo 🚀 多实例服务启动脚本
echo ==================================

echo 📋 可用的启动选项:
echo.
echo 1. 启动多个Gate服务实例
echo 2. 启动多个Lobby服务实例  
echo 3. 启动多个Game服务实例
echo 4. 启动所有服务的多个实例
echo 5. 自定义启动
echo.

set /p choice="请选择启动选项 (1-5): "

if "%choice%"=="1" goto start_gate_multi
if "%choice%"=="2" goto start_lobby_multi
if "%choice%"=="3" goto start_game_multi
if "%choice%"=="4" goto start_all_multi
if "%choice%"=="5" goto custom_start
goto invalid_choice

:start_gate_multi
echo.
echo 🚪 启动多个Gate服务实例...
echo 将在新窗口中启动以下实例:
echo   - Gate实例1: 服务器ID=1001, Web端口=4000, gRPC端口=14000
echo   - Gate实例2: 服务器ID=1002, Web端口=4001, gRPC端口=14001
echo   - Gate实例3: 服务器ID=1003, Web端口=4002, gRPC端口=14002
echo.

start "Gate实例1" cmd /k "start-gate.bat 1001 14000 4000"
timeout /t 3 /nobreak >nul
start "Gate实例2" cmd /k "start-gate.bat 1002 14001 4001"
timeout /t 3 /nobreak >nul
start "Gate实例3" cmd /k "start-gate.bat 1003 14002 4002"

echo ✅ Gate服务多实例启动完成！
goto end

:start_lobby_multi
echo.
echo 🏠 启动多个Lobby服务实例...
echo 将在新窗口中启动以下实例:
echo   - Lobby实例1: 服务器ID=2001, Web端口=5000, gRPC端口=15000
echo   - Lobby实例2: 服务器ID=2002, Web端口=5001, gRPC端口=15001
echo   - Lobby实例3: 服务器ID=2003, Web端口=5002, gRPC端口=15002
echo.

start "Lobby实例1" cmd /k "start-lobby.bat 2001 15000 5000"
timeout /t 3 /nobreak >nul
start "Lobby实例2" cmd /k "start-lobby.bat 2002 15001 5001"
timeout /t 3 /nobreak >nul
start "Lobby实例3" cmd /k "start-lobby.bat 2003 15002 5002"

echo ✅ Lobby服务多实例启动完成！
goto end

:start_game_multi
echo.
echo 🎮 启动多个Game服务实例...
echo 将在新窗口中启动以下实例:
echo   - Game实例1: 服务器ID=3001, Web端口=6000, gRPC端口=16000
echo   - Game实例2: 服务器ID=3002, Web端口=6001, gRPC端口=16001
echo   - Game实例3: 服务器ID=3003, Web端口=6002, gRPC端口=16002
echo.

start "Game实例1" cmd /k "start-game.bat 3001 16000 6000"
timeout /t 3 /nobreak >nul
start "Game实例2" cmd /k "start-game.bat 3002 16001 6001"
timeout /t 3 /nobreak >nul
start "Game实例3" cmd /k "start-game.bat 3003 16002 6002"

echo ✅ Game服务多实例启动完成！
goto end

:start_all_multi
echo.
echo 🌟 启动所有服务的多个实例...
echo 将在新窗口中启动以下实例:
echo.
echo Gate服务:
echo   - Gate实例1: 服务器ID=1001, Web端口=4000, gRPC端口=14000
echo   - Gate实例2: 服务器ID=1002, Web端口=4001, gRPC端口=14001
echo.
echo Lobby服务:
echo   - Lobby实例1: 服务器ID=2001, Web端口=5000, gRPC端口=15000
echo   - Lobby实例2: 服务器ID=2002, Web端口=5001, gRPC端口=15001
echo.
echo Game服务:
echo   - Game实例1: 服务器ID=3001, Web端口=6000, gRPC端口=16000
echo   - Game实例2: 服务器ID=3002, Web端口=6001, gRPC端口=16001
echo.

REM 启动Gate服务
start "Gate实例1" cmd /k "start-gate.bat 1001 14000 4000"
timeout /t 3 /nobreak >nul
start "Gate实例2" cmd /k "start-gate.bat 1002 14001 4001"
timeout /t 3 /nobreak >nul

REM 启动Lobby服务
start "Lobby实例1" cmd /k "start-lobby.bat 2001 15000 5000"
timeout /t 3 /nobreak >nul
start "Lobby实例2" cmd /k "start-lobby.bat 2002 15001 5001"
timeout /t 3 /nobreak >nul

REM 启动Game服务
start "Game实例1" cmd /k "start-game.bat 3001 16000 6000"
timeout /t 3 /nobreak >nul
start "Game实例2" cmd /k "start-game.bat 3002 16001 6001"

echo ✅ 所有服务多实例启动完成！
goto end

:custom_start
echo.
echo 🔧 自定义启动配置
echo ==================================
echo.
echo 请输入要启动的服务类型:
echo 1. Gate服务
echo 2. Lobby服务
echo 3. Game服务
echo.
set /p service_type="请选择服务类型 (1-3): "

if "%service_type%"=="1" set service_name=Gate&set script=start-gate.bat&set base_id=1001&set base_grpc=14000&set base_web=4000
if "%service_type%"=="2" set service_name=Lobby&set script=start-lobby.bat&set base_id=2001&set base_grpc=15000&set base_web=5000
if "%service_type%"=="3" set service_name=Game&set script=start-game.bat&set base_id=3001&set base_grpc=16000&set base_web=6000

if "%service_type%"=="1" goto custom_gate
if "%service_type%"=="2" goto custom_lobby
if "%service_type%"=="3" goto custom_game
goto invalid_choice

:custom_gate
set /p instance_count="请输入要启动的Gate实例数量 (1-5): "
set /p start_id="请输入起始服务器ID (默认1001): "
if "%start_id%"=="" set start_id=1001
goto custom_start_instances

:custom_lobby
set /p instance_count="请输入要启动的Lobby实例数量 (1-5): "
set /p start_id="请输入起始服务器ID (默认2001): "
if "%start_id%"=="" set start_id=2001
goto custom_start_instances

:custom_game
set /p instance_count="请输入要启动的Game实例数量 (1-5): "
set /p start_id="请输入起始服务器ID (默认3001): "
if "%start_id%"=="" set start_id=3001
goto custom_start_instances

:custom_start_instances
echo.
echo 🚀 启动 %service_name% 服务 %instance_count% 个实例...
echo 起始服务器ID: %start_id%
echo.

for /l %%i in (0,1,%instance_count%) do (
    if %%i gtr 0 (
        set /a current_id=%start_id% + %%i - 1
        set /a current_grpc=%base_grpc% + %%i - 1
        set /a current_web=%base_web% + %%i - 1
        
        echo 启动 %service_name% 实例 %%i: 服务器ID=!current_id!, Web端口=!current_web!, gRPC端口=!current_grpc!
        start "%service_name%实例%%i" cmd /k "%script% !current_id! !current_grpc! !current_web!"
        timeout /t 3 /nobreak >nul
    )
)

echo ✅ %service_name% 服务多实例启动完成！
goto end

:invalid_choice
echo ❌ 无效的选择，请重新运行脚本
goto end

:end
echo.
echo 📋 启动完成！
echo.
echo 💡 提示:
echo - 每个服务实例将在独立的命令行窗口中运行
echo - 可以通过Nacos控制台查看服务注册情况
echo - 服务之间会自动进行负载均衡
echo.
pause 